<?php
mb_internal_encoding('utf-8');
mb_regex_encoding('utf-8');
include_once "application/tools/Sql.php";
include_once "application/tools/Log.php";
include_once "application/models/Project.php";
include_once "application/models/ServicesData.php";
include_once "application/models/Member.php";
session_start();
Sql::open();

header('Content-type: application/json');

$pattern = "/\"0*(\d+(.\d+)?)\"((?!:))/";

// Si on souhaite afficher les donnees de service, il n'est pas necessaire de remplir le parametre 'column'
if (!empty($_GET['project']) && !empty($_GET['cards'])) {
    $servicesData = false;
    $column = null;
    $columns = null; 
	$project_id = htmlspecialchars($_GET['project']);
	
    if(!empty($_GET['column'])) {
    	$column = htmlspecialchars($_GET['column']);
    }
    
	if(!empty($_GET['columns'])) {
    	$columns = $_GET['columns'];
    }
    
	for ($i = 0; $i < count($columns); $i++) {
        $columns[$i] = htmlspecialchars($columns[$i]);
    }
    
    $cards = $_GET['cards'];
    
	for ($i = 0; $i < count($cards); $i++) {
        $cards[$i] = htmlspecialchars($cards[$i]);
    }
    
    if(!empty($_GET['servicesData']))
    {
    	$servicesData = htmlspecialchars($_GET['servicesData']);
    }
    
    $start = (!empty($_GET['ds'])) ? $_GET["ds"] : null;
    $end = (!empty($_GET['de'])) ? $_GET["de"] : null;

    if (($start != null) && ($end != null))
        $data = Project::getProjectTableContentFromDate($project_id, $cards, $columns, $start, $end, $servicesData);
    else
        $data = Project::getProjectTableContent($project_id, $cards, $column, $servicesData);
        
    echo preg_replace($pattern, '$1', json_encode($data));
    	
    //echo '{"00001235":[["01 Nov 2011",1],["02 Nov 2011",0.9],["03 Nov 2011",1],["04 Nov 2011",1],["05 Nov 2011",1.1],["06 Nov 2011",0.8],["07 Nov 2011",1.1],["08 Nov 2011",1],["09 Nov 2011",1],["10 Nov 2011",0.9],["11 Nov 2011",1.1],["12 Nov 2011",1],["13 Nov 2011",1.1],["14 Nov 2011",1],["15 Nov 2011",0],["16 Nov 2011",0],["17 Nov 2011",0],["18 Nov 2011",0],["19 Nov 2011",0],["20 Nov 2011",0]],"00001011":[["01 Nov 2011",0.9],["02 Nov 2011",1],["03 Nov 2011",1],["04 Nov 2011",1.1],["05 Nov 2011",1.1],["06 Nov 2011",1],["07 Nov 2011",0.9],["08 Nov 2011",0.8],["09 Nov 2011",0.9],["10 Nov 2011",1],["11 Nov 2011",1.1],["12 Nov 2011",0.9],["13 Nov 2011",1.2],["14 Nov 2011",1],["15 Nov 2011",0],["16 Nov 2011",0],["17 Nov 2011",0],["18 Nov 2011",0],["19 Nov 2011",0],["20 Nov 2011",0]]}';
}

if(!empty($_GET['checkProject'])){
	echo Project::checkAvailability(htmlspecialchars($_GET['checkProject']));
}
    
if(!empty($_GET['checkUser'])){
    echo Member::checkAvailability(htmlspecialchars($_GET['checkUser']));
}

if(!empty($_GET['servicesId'])) {
	echo json_encode(ServicesData::getServicesData(htmlspecialchars($_GET['servicesId'])));
}
?>